const path = require('path')
const fs = require('fs-extra')
const HtmlWebPackPlugin = require('html-webpack-plugin');

/**
* 动态查找所有入口文件
*/
function GetFolderNameArr(path, bool) {
    let components = []
    const files = fs.readdirSync(path)
    //console.log(files, "========files==========");
    files.forEach(function (item, index) {
        let stat = fs.lstatSync(path + item)
        if (stat.isDirectory() === bool) {
            components.push(item)
        }
    })
    return components;
}



const HTML_META = {
    charset: { charset: 'utf-8' },
    'Content-Security-Policy': {
        'http-equiv': 'Content-Security-Policy',
        content:
            "default-src *;style-src 'self' 'unsafe-inline';script-src 'self' 'unsafe-inline' 'unsafe-eval';img-src 'self' data: http: https:;"
    },
    'X-UA-Compatible': { 'http-equiv': 'X-UA-Compatible', content: 'ie=edge' },
    viewport: 'width=device-width, initial-scale=1, shrink-to-fit=no',
    renderer: 'webkit',
    'format-detection': 'telephone=no'
};

function getConfig() {
    let entrys = {}  //设置多页面入口
    let htmlPlugin = []

    var folderNameArr = GetFolderNameArr('./entry/', true)

    folderNameArr.forEach(key => {
        entrys[key] = `./entry/${key}/index.js`

        let plugin = new HtmlWebPackPlugin({
            template: `./entry/${key}/index.html`,
            filename: `${key}.html`,
            chunks: [key],
            meta: HTML_META
        });
        htmlPlugin.push(plugin)
    });


    return { entrys, htmlPlugin }
}

var config = getConfig()

fs.writeFile(path.join(__dirname, '../log/config/util.json'), JSON.stringify(config, null, '\t'), function () { });

module.exports = {
    ...config
}